package com.yangzc.lion.common.dao;

import java.util.HashMap;
import java.util.Map;

/**
 * HQL语句的信息描述。<br>
 * 
 * @author 叶中奇
 * @version 1.0 2006-04-02
 */
public class HQLInfo implements Cloneable {
	/**
	 * 系统自己判断是否需要过滤
	 */
	public final static int DISTINCT_AUTO = 2;

	/**
	 * 不过滤重复值
	 */
	public final static int DISTINCT_NO = 0;

	/**
	 * 过滤重复值
	 */
	public final static int DISTINCT_YES = 1;

	private boolean autoFetch = true;

	private int distinctType = DISTINCT_AUTO;

	private String fromBlock = null;

	private boolean getCount = true;

	private boolean gettingCount = false;

	private String joinBlock = null;

	private String modelName = null;

	private String orderBy = null;

	private int pageNo = 1;

	private Map<String, Object> parameterMap = new HashMap<String, Object>();

	private int pageSize = 15;

	private String selectBlock = null;

	private String whereBlock = null;

	private String modelTable;

	public Object clone() throws CloneNotSupportedException {
		HQLInfo hqlInfo = (HQLInfo) super.clone();
		hqlInfo.parameterMap = new HashMap<String, Object>(parameterMap);
		return hqlInfo;
	}

	public int getDistinctType() {
		return distinctType;
	}

	public String getFromBlock() {
		return fromBlock;
	}

	public String getJoinBlock() {
		return joinBlock;
	}

	public String getModelName() {
		return modelName;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public int getPageNo() {
		return pageNo;
	}

	public Map<String, Object> getParameterMap() {
		return parameterMap;
	}

	public String getSelectBlock() {
		return selectBlock;
	}

	public String getWhereBlock() {
		return whereBlock;
	}

	public boolean isAutoFetch() {
		return autoFetch;
	}

	public boolean isGetCount() {
		return getCount;
	}

	protected boolean isGettingCount() {
		return gettingCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 设置查询时自动过滤重复数据的类型
	 * 
	 * @param distinctType
	 */
	public void setDistinctType(int distinctType) {
		this.distinctType = distinctType;
	}

	/**
	 * 设置from语句
	 * 
	 * @param fromBlock
	 */
	public void setFromBlock(String fromBlock) {
		this.fromBlock = fromBlock;
	}

	public void setGetCount(boolean getCount) {
		this.getCount = getCount;
	}

	protected void setGettingCount(boolean gettingCount) {
		this.gettingCount = gettingCount;
	}

	/**
	 * 设置是否自动展开相关的字段信息
	 * 
	 * @param autoFetch
	 */
	public void setIsAutoFetch(boolean autoFetch) {
		this.autoFetch = autoFetch;
	}

	/**
	 * 设置紧跟在from语句后面的join语句
	 * 
	 * @param joinBlock
	 */
	public void setJoinBlock(String joinBlock) {
		this.joinBlock = joinBlock;
	}

	/**
	 * 设置域模型名称
	 * 
	 * @param modelName
	 */
	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	/**
	 * 设置排序字段
	 * 
	 * @param orderBy
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * 设置从第几页开始显示
	 * 
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setParameter(String key, Object value) {
		parameterMap.put(key, value);
	}

	public void setParameter(Map<String, Object> parameterMap) {
		this.parameterMap.putAll(parameterMap);
	}

	/**
	 * 设置select语句，不设置则返回域模型
	 * 
	 * @param selectBlock
	 */
	public void setSelectBlock(String selectBlock) {
		this.selectBlock = selectBlock;
	}

	/**
	 * 设置where语句
	 * 
	 * @param whereBlock
	 */
	public void setWhereBlock(String whereBlock) {
		this.whereBlock = whereBlock;
	}

	public String getModelTable() {
		if (modelTable == null)
			modelTable = getAsTableName(getModelName());
		return modelTable;
	}

	private String getAsTableName(String mainModel) {
		int i = mainModel.lastIndexOf('.');
		if (i > -1)
			mainModel = mainModel.substring(i + 1);
		return mainModel.substring(0, 1).toLowerCase() + mainModel.substring(1);
	}
}
